package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.model.Player;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateLoginMapper;
import com.originskyseed.platform.user.model.PlayerRelateLogin;
import com.originskyseed.platform.user.service.IPlayerRelateLoginService;

/**
 * 玩家登录信息表Service接口实现类（OSI_PLAYER_RELATE_LOGIN）
 *
 * @author wlj
 */
@DubboService
@Component("playerRelateLoginService")
public class PlayerRelateLoginServiceImpl implements IPlayerRelateLoginService {

	@Resource
	private PlayerRelateLoginMapper playerRelateLoginMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateLoginMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateLoginMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateLogin record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLoginMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateLogin record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLoginMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateLogin record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLoginMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateLogin record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLoginMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateLogin selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateLoginMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLogin> selectList(PlayerRelateLogin record) throws Exception {
		return this.playerRelateLoginMapper.selectList(record);
	}

	/**
	 * 通过账号查询列表
	 *
	 * @param account
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLogin> selectListByAccout(String account) throws Exception {
		return this.playerRelateLoginMapper.selectListByAccout(account);
	}

	/**
	 * 统计账号登录次数
	 * 
	 * @param account
	 * @return
	 * @throws Exception
	 */
	@Override
	public int loginCountByAccout(String account) throws Exception {
		return this.playerRelateLoginMapper.loginCountByAccout(account);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateLogin> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateLogin> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateLogin> list = this.playerRelateLoginMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询活跃登录用户列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateLogin> selectActiveLoginUserByPage(Map<String, Object> params, BasePage<PlayerRelateLogin> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateLogin> list = this.playerRelateLoginMapper.selectActiveLoginUserByPage(params);
		if(list!=null&&list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				PlayerRelateLogin playerRelateLogin=list.get(i);
				String playerId=playerRelateLogin.getPlayerId();
				Date registerTime=playerRelateLogin.getRegisterTime();
				Date loginTime=playerRelateLogin.getLoginTime();
				long day=0;
				if(registerTime!=null&&loginTime!=null) {
					day = (loginTime.getTime() - registerTime.getTime()) / (24 * 60 * 60 * 1000);
				}
				int loginCount=playerRelateLoginMapper.loginCountByPlayerId(playerId);
				playerRelateLogin.setLoginCount(loginCount);
				playerRelateLogin.setRegisterDay((int)day);
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertBatch(List<PlayerRelateLogin> list) throws Exception {
		return this.playerRelateLoginMapper.insertBatch(list);
	}


	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<PlayerRelateLogin> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertBatch(list);
	}

	/**
	 * 查询玩家id和时间是否存在
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLogin> selectPlayerIds(List<PlayerRelateLogin> list) throws Exception {
		return this.playerRelateLoginMapper.selectPlayerIds(list);
	}


	/**
	 * 查询玩家id和时间是否存在
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLogin> selectPlayerIds(String tenantCode,List<PlayerRelateLogin> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return  selectPlayerIds(list);
	}
}
